#region Copyright (c) 2006-2010 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2010 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Widgetsphere.Generator.Models;
using Widgetsphere.Generator.ServiceInterfaces.ProjectItemGenerators;
using Widgetsphere.Generator.Common.Util;
using Widgetsphere.Generator.ProjectItemGenerators;

namespace Widgetsphere.Generator.ServiceInterfaces.ProjectItemGenerators.InterfaceMain
{
	class InterfaceMainTemplate : BaseServiceInterfacesTemplate
	{
		private StringBuilder sb = new StringBuilder();
		private Table _currentTable = null;

		#region Constructors
		public InterfaceMainTemplate(ModelRoot model, Table table)
		{
			_model = model;
			_currentTable = table;
		}
		#endregion

		#region BaseClassTemplate overrides
		public override string FileContent
		{
			get
			{
				GenerateContent();
				return sb.ToString();
			}
		}

		public override string FileName
		{
			get { return "I" + _currentTable.PascalName + ".Generated.cs"; }
		}

		public string ParentItemName
		{
			get { return "I" + _currentTable.PascalName + ".cs"; }
		}

		#endregion

		#region GenerateContent

		private void GenerateContent()
		{
			try
			{
				ValidationHelper.AppendCopyrightInCode(sb, _model);
				this.AppendUsingStatements();
				sb.AppendLine("namespace " + DefaultNamespace);
				sb.AppendLine("{");
				this.AppendClass();
				sb.AppendLine("}");
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		#endregion

		#region namespace / objects

		public void AppendUsingStatements()
		{
			sb.AppendLine("using System;");
			sb.AppendLine("using System.Collections.Generic;");
			sb.AppendLine("using System.Linq;");
			sb.AppendLine("using System.Text;");
			sb.AppendLine("using " + _model.CompanyName + "." + _model.ProjectName + ".DataTransfer;");
			sb.AppendLine();
		}

		public void AppendClass()
		{
			sb.AppendLine("	/// <summary>");
			sb.AppendLine("	/// The service interface for the '" + _currentTable.PascalName + "' object type");
			sb.AppendLine("	/// </summary>");
			if (!_currentTable.Immutable)
				sb.AppendLine("	public partial interface I" + _currentTable.PascalName + "Service : IPersistableService<" + _currentTable.PascalName + "DTO>");
			else
				sb.AppendLine("	public partial interface I" + _currentTable.PascalName + "Service : IService<" + _currentTable.PascalName + "DTO>");

			sb.AppendLine("	{");
			this.AppendMethodSelectByPK();
			this.AppendMethodSelectByRelation();
			this.AppendMethodSelectByField();
			//this.AppendMethodPersist();
			//this.AppendMethodDelete();
			sb.AppendLine("	}");
		}

		private void AppendMethodSelectByPK()
		{
			sb.AppendLine("		/// <summary>");
			sb.AppendLine("		/// Return a specific " + _currentTable.PascalName + " object by primary key");
			sb.AppendLine("		/// </summary>");
			sb.Append("		" + _currentTable.PascalName + "DTO SelectByPrimaryKey(");

			int ii = 0;
			foreach (Column column in _currentTable.PrimaryKeyColumns.OrderBy(x => x.Name))
			{
				sb.Append(column.GetCodeType() + " " + column.CamelName);
				if (ii < _currentTable.PrimaryKeyColumns.Count - 1)
					sb.Append(", ");
				ii++;
			}
			sb.AppendLine(");");
			sb.AppendLine();
		}

		private void AppendMethodSelectByRelation()
		{
			//Get by parent relations
			foreach (Relation relation in _currentTable.GetRelations().Where(x => x.IsGenerated))
			{
				Table parentTable = (Table)relation.ParentTableRef.Object;
				Table childTable = (Table)relation.ChildTableRef.Object;

				//Do not render relations from inheritance
				if (!childTable.IsInheritedFrom(parentTable) && childTable.Generated)
				{
					sb.AppendLine("		/// <summary>");
					sb.AppendLine("		/// Return a list of related " + childTable.PascalName + " objects");
					sb.AppendLine("		/// </summary>");
					sb.AppendLine("		List<" + childTable.PascalName + "DTO> Get" + relation.PascalRoleName + childTable.PascalName + "List(" + _currentTable.PascalName + "DTO item);");
					sb.AppendLine();
				}
			}

			//Get by child relations
			foreach (Relation relation in _currentTable.GetChildRoleRelationsFullHierarchy().Where(x => x.IsGenerated))
			{
				Table parentTable = (Table)relation.ParentTableRef.Object;
				Table childTable = (Table)relation.ChildTableRef.Object;

				//Do not render relations from inheritance
				if (!childTable.IsInheritedFrom(parentTable))
				{
					sb.AppendLine("		/// <summary>");
					sb.AppendLine("		/// Return the related " + parentTable.PascalName + " item");
					sb.AppendLine("		/// </summary>");
					sb.AppendLine("		" + parentTable.PascalName + "DTO Get" + relation.PascalRoleName + parentTable.PascalName + "Item(" + _currentTable.PascalName + "DTO item);");
					sb.AppendLine();
				}
			}

		}

		private void AppendMethodSelectByField()
		{
			foreach (Column column in _currentTable.GetColumnsFullHierarchy().Where(x => x.Generated && x.IsSearchable))
			{				
				sb.AppendLine("		/// <summary>");
				sb.AppendLine("		/// Return a list of items by the " + column.PascalName + " field");
				sb.AppendLine("		/// </summary>");
				sb.AppendLine("		List<" + _currentTable.PascalName + "DTO> SelectBy" + column.PascalName + "(" + _currentTable.PascalName + "DTO item, " + column.GetCodeType() + " " + column.CamelName + ");");
				sb.AppendLine();
			}

		}

		//private void AppendMethodPersist()
		//{
		//  if (!_currentTable.Immutable)
		//  {
		//    sb.AppendLine("		/// <summary>");
		//    sb.AppendLine("		/// Persist a single object");
		//    sb.AppendLine("		/// </summary>");
		//    sb.AppendLine("		bool Persist(" + _currentTable.PascalName + "DTO item);");
		//    sb.AppendLine();
		//    sb.AppendLine("		/// <summary>");
		//    sb.AppendLine("		/// Persist a single object");
		//    sb.AppendLine("		/// </summary>");
		//    sb.AppendLine("		bool Persist(List<" + _currentTable.PascalName + "DTO> list);");
		//    sb.AppendLine();
		//  }
		//}

		//private void AppendMethodDelete()
		//{
		//  if (!_currentTable.Immutable)
		//  {
		//    sb.AppendLine("		/// <summary>");
		//    sb.AppendLine("		/// Delete the specified object");
		//    sb.AppendLine("		/// </summary>");
		//    sb.AppendLine("		bool Delete(" + _currentTable.PascalName + "DTO item);");
		//    sb.AppendLine();
		//  }
		//}

		#endregion


	}
}